<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

use Webman\Route;

// 用户相关路由
Route::group('/api/user', function () {
    Route::post('/create-or-update', [app\controller\UserController::class, 'createOrUpdate']);
    Route::get('/info', [app\controller\UserController::class, 'getInfo']);
    Route::get('/room-history', [app\controller\UserController::class, 'getRoomHistory']);
    Route::post('/update-total-score', [app\controller\UserController::class, 'updateTotalScore']);
    Route::post('/update-room-info', [app\controller\UserController::class, 'updateRoomInfo']);
    Route::post('/upload-avatar', [app\controller\UserController::class, 'uploadAvatar']);
    Route::get('/diagnose-connection', [app\controller\UserController::class, 'diagnoseConnection']);
    Route::get('/stats', [app\controller\UserStatsController::class, 'getStats']);
    Route::get('/records', [app\controller\UserStatsController::class, 'getRecords']);
});

// 房间相关路由
Route::group('/api/room', function () {
    Route::post('/create', [app\controller\RoomController::class, 'create']);
    Route::post('/join', [app\controller\RoomController::class, 'join']);
    Route::get('/get-by-code', [app\controller\RoomController::class, 'getByCode']);
    Route::get('/list', [app\controller\RoomController::class, 'list']);
    Route::post('/update-status', [app\controller\RoomController::class, 'updateStatus']);
    Route::get('/qrcode', [app\controller\RoomController::class, 'generateQRCode']);
    Route::get('/user-qrcode', [app\controller\RoomController::class, 'generateUserQRCode']);
    Route::get('/universal-qrcode', [app\controller\RoomController::class, 'generateUniversalQRCode']);
    Route::post('/exit', [app\controller\RoomController::class, 'exit']);
    Route::post('/roomStatus', [app\controller\RoomController::class, 'roomStatus']);
    Route::post('/set-tea-meal', [app\controller\RoomController::class, 'setTeaMeal']);
    Route::get('/tea-meal-stats', [app\controller\RoomController::class, 'getTeaMealStats']);
});

// 广告相关路由
Route::group('/api/ad', function () {
    Route::get('/list', [app\controller\AdController::class, 'list']);
    Route::post('/click', [app\controller\AdController::class, 'click']);
});

// 记账相关路由
Route::group('/api/accounting', function () {
    Route::get('/room-records', [app\controller\AccountingController::class, 'getRoomRecords']);
    Route::post('/add-record', [app\controller\AccountingController::class, 'addRecord']);
    Route::get('/user-settlement', [app\controller\AccountingController::class, 'getUserSettlement']);
    Route::get('/room-settlement', [app\controller\AccountingController::class, 'getRoomSettlement']);
    Route::post('/send-settlement-to-room', [app\controller\AccountingController::class, 'sendSettlementToRoom']);
    // 新增交易流水查询API
    Route::get('/transaction-history', [app\controller\AccountingController::class, 'getTransactionHistory']);
    Route::get('/user-score-history', [app\controller\AccountingController::class, 'getUserScoreHistory']);
    Route::get('/user-room-records', [app\controller\AccountingController::class, 'getUserRoomRecords']);
    Route::get('/user-room-history', [app\controller\AccountingController::class, 'getUserRoomHistory']);
    // 自动结算API
    Route::post('/trigger-auto-settlement', [app\controller\AccountingController::class, 'triggerAutoSettlement']);
    // 手动结算API
    Route::post('/room-settlement-and-notify', [app\controller\AccountingController::class, 'getRoomSettlementAndNotify']);
    // 个人结算API
    Route::post('/personal-settlement', [app\controller\AccountingController::class, 'personalSettlement']);
    // 获取结算建议API
    Route::get('/settlement-suggestion', [app\controller\AccountingController::class, 'getSettlementSuggestion']);
});

// 静态文件路由 - 头像访问
Route::get('/uploads/avatars/{filename}', function ($filename) {
    $filePath = public_path('uploads/avatars/' . $filename);
    if (file_exists($filePath)) {
        $mimeType = mime_content_type($filePath);
        return response()->file($filePath, [
            'Content-Type' => $mimeType,
            'Cache-Control' => 'public, max-age=31536000'
        ]);
    }
    return response('File not found', 404);
});

// 健康检查
Route::get('/health', function () {
    return json(['status' => 'ok', 'timestamp' => time()]);
});

// 分享相关路由
Route::group('/api/share', function () {
    Route::post('/config', [app\controller\ShareController::class, 'getConfig']);
});

// 排行榜相关路由
Route::group('/api/leaderboard', function () {
    Route::get('/list', [app\controller\LeaderboardController::class, 'getLeaderboard']);
    Route::get('/user-rank', [app\controller\LeaderboardController::class, 'getUserRank']);
});

// 时间轴相关路由
Route::group('/api/timeline', function () {
    Route::get('/user-timeline', [app\controller\TimelineController::class, 'getUserTimeline']);
    Route::get('/user-stats', [app\controller\TimelineController::class, 'getUserStats']);
    Route::get('/settlement-detail', [app\controller\TimelineController::class, 'getRoomSettlementDetail']);
});

// 结算策略相关路由
Route::group('/api/settlement-strategy', function () {
    Route::get('/user-strategy', [app\controller\SettlementStrategyController::class, 'generateUserStrategy']);
    Route::get('/room-strategy', [app\controller\SettlementStrategyController::class, 'generateRoomStrategy']);
    Route::get('/optimal-strategy', [app\controller\SettlementStrategyController::class, 'generateOptimalStrategy']);
    Route::post('/save-strategy', [app\controller\SettlementStrategyController::class, 'saveSettlementStrategy']);
    Route::post('/save-user-strategy', [app\controller\SettlementStrategyController::class, 'saveUserSettlementStrategy']);
    Route::post('/finalize-settlement', [app\controller\SettlementStrategyController::class, 'finalizeSettlement']);
    Route::get('/user-history', [app\controller\SettlementStrategyController::class, 'getUserSettlementHistory']);
});


